function loadStorehouse(cb) {
  request.getBagInfo(function(res) {
    player.bag = res.bag
    player.basicInfo.money = res.money
    player.basicInfo.gold = res.gold
    $('.append').remove()
    $('#mainContent').append(template('t_storehouse'))
    $('#mainContent').addClass('bigMainContent')
    $('#messageMain').addClass('litterMessageMain')
    if (cb)
      cb()
    exit()
    var part, bagIndex, flag = 0
    var sellItem = null
    setItems()

    setActionDisplay(0, 0, 0, 0, 1)
    $('.bSize').html(player.bag.size)
    $('.bag').click(function() {
      var index = $(this).attr('value')
      var item = player.bag.items[index]
      $('.bag,.equipment').removeClass('select')
      if (index === bagIndex && flag === 1) {
        setActionDisplay(0, 0, 0, 0, 1)
        bagIndex = -1
        return
      }
      if (item) {
        bagIndex = index
        $(this).addClass('select')
        var type = Math.floor(item.templateId / 10000)
        if (type < 50) { //道具
          getItemInfo(item)
          setActionDisplay(0, 1, 0, 1, 0, 1)
          if (templateConfig.total[item.templateId].propType) {
            $('.useProp').css('display', 'inline-block')
          }
          if (item.bind === 1) {
            $('.sell').css('display', 'inline-block')
            sellItem = item
          }
        } else { //装备
          getEquipmentInfo(item)
          part = equipmentsPartConfig[type]
          setActionDisplay(1, 1, 0, 1, 0, 1)
        }
      } else {
        bagIndex = -1
        setActionDisplay(0, 0, 0, 0, 1)
      }
      flag = 1
    })

    setEquipments()

    $('.equipment').click(function() {
      var index = $(this).attr('value')
      $('.bag,.equipment').removeClass('select')
      if (index === part && flag === 2) {
        setActionDisplay(0, 0, 0, 0, 1)
        part = -1
        return
      }
      if (index && player.equipments[index]) {
        part = index
        $(this).addClass('select')
        var equipment = player.equipments[part]
        getEquipmentInfo(equipment)
        setActionDisplay(0, 0, 1, 1, 0, 1)
      } else {
        part = -1
        setActionDisplay(0, 0, 0, 0, 1)
      }
      flag = 2
    })

    //装备
    $('.equip').click(function() {
      request.equip(bagIndex, function() {
        loadStorehouse()
        $('.bag,.equipment').removeClass('select')
        $('.' + part).addClass('select')
      })
    })

    //丢弃
    $('.discard').click(function() {
      $('.bag,.equipment').removeClass('select')
      request.discard(bagIndex, function() {
        loadStorehouse()
      })
    })

    //卸下
    $('.disEquipment').click(function() {
      request.disEquipment(part, function(res) {
        if (res.error === 0) {
          $('.bag,.equipment').removeClass('select')
          loadStorehouse()
        } else if (res.error === 1) {
          showMessage('背包空间不足')
        }
      })
    })

    //整理仓库
    $('.arrange').click(function() {
      request.arrange(function() {
        loadStorehouse(function() {
          $('.arrange').remove()
        })
      })
    })

    //领取补给品
    $('.takeAlms').click(function() {
      request.takeAlms(function(res) {
        if (res.error === 0) {
          showMessage('你领取了补给品,获得了' + propName[res.propId] + 'X' + res.num)
          loadStorehouse()
        } else if (res.error === 1) {
          showMessage('仓库没空间了,无法领取补给品')
        } else if (res.error === 2) {
          showMessage('每天只能领取15次')
        } else if (res.error === 3) {
          showMessage('你还有治疗药水,无法领取')
        }
      })
    })

    //扩展背包
    $('.addBagSize').click(function() {
      request.addBagSize(function(res) {
        if (res.error === 1) {
          showMessage('藤曼不足')
        } else if (res.error === 2) {
          showMessage('银币不足')
        } else if (res.error === 3) {
          showMessage('藤曼不足')
        } else if (res.error === 4) {
          showMessage('金币不足')
        } else if (res.error === 0) {
          showMessage('背包空间+1')
          loadStorehouse()
        } else if (res.error === 5) {
          showMessage('无法再扩展空间')
        }
      })
    })

    //使用
    $('.useProp').click(function() {
      request.useProp(bagIndex, function(res) {
        if (res.error === 0) {
          if (res.getMoney) {
            showMessage('获得了 ' + res.getMoney + '银币')
          }
          if (res.getGold) {
            showMessage('获得了 ' + res.getGold + '金币')
          }
          if (res.props) {
            var props = res.props
            for (var i in props) {
              showMessage('获得了 ' + propName[props[i][0]] + ' X ' + props[i][1])
            }
          }
          loadStorehouse()
        }else if(res.error === 3){
          showMessage('背包空间不足,请清理背包')
        }
      })
    })

    //出售
    $('.sell').click(function() {
      setSellPanel(sellItem)
    })
  })

  function setItems() {
    var height = $(window).height()
    var html = ''
    for (var i = 0; i < player.bag.size; i++) {
      html += '<div class="bag' + i + ' bag" value="' + i + '"></div>'
    }
    $('#storehouse .house').html(html)
    var items = player.bag.items
    for (var i in items) {
      var item = items[i]
      if (item) {
        var itemConfig = templateConfig.total[item.templateId]
        if (item.name) {
          $('.bag' + i).html(item.name + '▪' + propName[item.templateId])
        } else if (templateConfig.total[item.templateId].maxstack === 1) {
          $('.bag' + i).html(propName[item.templateId])
        } else {
          $('.bag' + i).html(propName[item.templateId] + 'X' + item.stack)
        }
        if (item.quality === 2) {
          $('.bag' + i).addClass('itemColor1')
        } else if (item.quality === 3) {
          $('.bag' + i).addClass('itemColor2')
        } else if (item.quality === 4) {
          $('.bag' + i).addClass('itemColor3')
        }

        if (itemConfig.quality === 2) {
          $('.bag' + i).addClass('itemColor1')
        } else if (itemConfig.quality === 3) {
          $('.bag' + i).addClass('itemColor2')
        } else if (itemConfig.quality === 4) {
          $('.bag' + i).addClass('itemColor3')
        }

      } else {
        $('.bag' + i).html('')
      }
    }
    $('.money').html(player.basicInfo.money)
    $('.gold').html(player.basicInfo.gold)
    var bagConfig = templateConfig.other[290030]
    if (player.bag.size < (bagConfig.free + bagConfig.money)) {
      var size = player.bag.size - bagConfig.free
      var config = templateConfig.other[290031]
      var needMoney = config.needMoney[0] * size * 2 + config.needMoney[1]
      var name = propName[config.prop[0]]
      var propNum = config.prop[1] * size + config.prop[2]
      $('.need').html('银币 ' + needMoney + '<br>' + name + 'X' + propNum)
    } else if (player.bag.size < (bagConfig.free + bagConfig.money + bagConfig.gold)) {
      var size = player.bag.size - bagConfig.free - bagConfig.money
      var config = templateConfig.other[290032]
      var needGold = config.needGold[0] * size + config.needGold[1]
      var name = propName[config.prop[0]]
      var propNum = config.prop[1] * size + config.prop[2]
      $('.need').html('金币 ' + needGold + '<br>' + name + 'X' + propNum)
    } else {
      $('.addBagSize').css('display', 'none')
    }
  }

  function setActionDisplay(flag1, flag2, fag3, flag4, flag5, flag6, flag7, flag8) {
    if (flag1) {
      $('.equip').css('display', 'inline-block')
    } else {
      $('.equip').css('display', 'none')
    }

    if (flag2) {
      $('.discard').css('display', 'inline-block')
    } else {
      $('.discard').css('display', 'none')
    }

    if (fag3) {
      $('.disEquipment').css('display', 'inline-block')
    } else {
      $('.disEquipment').css('display', 'none')
    }

    if (flag4) {
      $('.equipmentInfo').css('display', 'block')
    } else {
      $('.equipmentInfo').css('display', 'none')
    }

    if (flag5) {
      $('.info2').css('display', 'block')
    } else {
      $('.info2').css('display', 'none')
    }

    if (flag6) {
      $('#storehouse .info .con').css('display', 'block')
    } else {
      $('#storehouse .info .con').css('display', 'none')
    }

    if (flag7) {
      $('#storehouse .useProp').css('display', 'inline-block')
    } else {
      $('#storehouse .useProp').css('display', 'none')
    }

    if (flag8) {
      $('#storehouse .sell').css('display', 'inline-block')
    } else {
      $('#storehouse .sell').css('display', 'none')
    }

  }

  function getItemInfo(item) {
    $('.equipmentInfo .title').html('<span>' + propName[item.templateId] + '</span>')
    $('.equipmentInfo .info .con1').empty()
    $('.equipmentInfo .info .con2').empty()
    $('.equipmentInfo .info .con3').empty()
    $('.equipmentInfo .info .con4').empty()
    var itemConfig = templateConfig.total[item.templateId]

    if (itemConfig.quality === 2) {
      $('.equipmentInfo .title span').addClass('itemColor1')
    } else if (itemConfig.quality === 3) {
      $('.equipmentInfo .title span').addClass('itemColor2')
    } else if (itemConfig.quality === 4) {
      $('.equipmentInfo .title span').addClass('itemColor3')
    }


    var con = ''
    if (item.bind === 0) {
      con += '[不可交易]<br>'
    } else {
      con += '[可交易]<br>'
    }
    var type = Math.floor(item.templateId / 10000)
    if (type == 22) {
      con += '[类型 附魂书]' + '<br>'
    } else if (type == 21) {
      con += '[类型 材料]' + '<br>'
    } else if (type == 25) {
      con += '[类型 宝石]' + '<br>'
    }

    if (itemConfig.quality === 1) {
      con += ' [品质 普通]' + '<br>'
    } else if (itemConfig.quality === 2) {
      con += ' [品质 精良]' + '<br>'
    } else if (itemConfig.quality === 3) {
      con += ' [品质 稀有]' + '<br>'
    } else if (itemConfig.quality === 4) {
      con += ' [品质 传承]' + '<br>'
    }
    $('.equipmentInfo .info .con1').html(con)

    if (itemConfig.desc) {
      $('.equipmentInfo .info .con4').append('<span class="desc">' + itemConfig.desc + '</span>')
    }


  }
}

//设置装备栏
function setEquipments(cb) {
  request.getEquipments(function(res) {
    player.equipments = res.equipments
    equipments = res.equipments
    for (var i in equipments) {
      var equipment = equipments[i]
      if (equipment) {
        if (equipment.strengthenLvl) {
          $('.' + i).html(equipment.name + '▪' + propName[equipment.templateId] + ' +' + equipment.strengthenLvl)
        } else {
          $('.' + i).html(equipment.name + '▪' + propName[equipment.templateId])
        }
      } else {
        $('.' + i).html('')
      }

      if (equipment && equipment.quality === 2) {
        $('.' + i).addClass('itemColor1')
      } else if (equipment && equipment.quality === 3) {
        $('.' + i).addClass('itemColor2')
      } else if (equipment && equipment.quality === 4) {
        $('.' + i).addClass('itemColor3')
      }
    }
    if (cb)
      cb()
  })
}


//获得装备信息
function getEquipmentInfo(equipment) {
  $('.equipmentInfo .title').html('<span>' + equipment.name + '▪' + propName[equipment.templateId] + '</span>')
  if (equipment.quality === 2) {
    $('.equipmentInfo .title span').addClass('itemColor1')
  } else if (equipment.quality === 3) {
    $('.equipmentInfo .title span').addClass('itemColor2')
  } else if (equipment.quality === 4) {
    $('.equipmentInfo .title span').addClass('itemColor3')
  }

  $('.equipmentInfo .info .con1').empty()
  $('.equipmentInfo .info .con2').empty()
  $('.equipmentInfo .info .con3').empty()
  $('.equipmentInfo .info .con4').empty()
  var mainAttri = equipment.mainAttri
  var con = ''
  con += '[装备等级 ' + equipment.lvl + ']' + '<br>'
  if (equipment.strengthenLvl) {
    con += ' [强化等级 ' + equipment.strengthenLvl + ']' + '<br>'
  }

  if (equipment.quality === 1) {
    con += ' [品质 普通]' + '<br>'
  } else if (equipment.quality === 2) {
    con += ' [品质 精良]' + '<br>'
  } else if (equipment.quality === 3) {
    con += ' [品质 稀有]' + '<br>'
  } else if (equipment.quality === 4) {
    con += ' [品质 传承]' + '<br>'
  }

  if (equipment.solt && equipment.solt[0]) {
    con += '[宝石 ' + propName[equipment.solt[0]] + ']' + '<br>'
  }
  $('.equipmentInfo .info .con1').html(con)
  for (var i in mainAttri) {
    $('.equipmentInfo .info .con2').append('<div class="mainAttri">' + attriName[i] + '  ' + mainAttri[i] + attriNameSuffix[i] + '<br></div>')
  }
  if (equipment.specialAttri) {
    $('.equipmentInfo .info .con2').append('<div class="specialAttri"></div>')
    for (var i in equipment.specialAttri) {
      $('.specialAttri').append(attriName[i] + '  ' + equipment.specialAttri[i] + attriNameSuffix[i] + '<br>')
    }
  }
  var addAttri = equipment.addAttri
  if (addAttri) {

    $('.equipmentInfo .info .con2').append('<div class="addAttri"></div>')
    for (var i in addAttri) {
      var plus = templateConfig.strengthen[440000][i] * equipment.strengthenLvl
      $('.addAttri').append(attriName[i] + '  ' + (addAttri[i] + plus) + attriNameSuffix[i] + '<br>')
    }
  }

  if (equipment.skills) {
    for (var i in equipment.skills) {
      var con = '<span class="icon-' + skillsSvgs[equipment.skills[i]] + '"></span>'
      $('.equipmentInfo .info .con4').append(con)
    }
  }
}

function setSellPanel(sellItem) {
  $('#mainContent').append(template('t_wholeBg'))
  $('#mainContent').append(template('t_sellPanel'))
  $('.itemName').html(propName[sellItem.templateId])
  $('#sellPanel .num').val(sellItem.stack)

  $('.sellToTrade').click(function() {
    var sellGold = parseInt($('#sellPanel .sellGold').val())
    var num = parseInt($('#sellPanel .num').val())
    if (!sellGold || !num || sellGold <= 0 || num <= 0 || num > sellItem.stack) {
      showMessage('出售到交易所失败')
      $('#mainContent #wholeBg').remove()
      $('#mainContent #sellPanel').remove()
    }
    request.sellToTrade(sellItem.pos, sellGold, num, function(res) {
      if (res.error === 4) {
        showMessage('你交易所上架的物品太多了,无法再出售')
      } else if (res.error === 0) {
        showMessage('出售到交易所成功')
        loadStorehouse()
      } else {
        showMessage('出售到交易所失败')
      }
      $('#mainContent #wholeBg').remove()
      $('#mainContent #sellPanel').remove()
    })
  })

  $('#wholeBg,#sellPanel .cancel').click(function() {
    $('#wholeBg, #sellPanel').remove()
  })
}
